var view = document.getElementsByTagName('body')[0].id;
if (view == 'island') {
islandView();
}

function islandView() {
	function registerCity(ul) {
		function item(p) {
			var li = $X('li[@class="'+ city[p] +'"]/span/following::text()[1]', ul);
			return li && trim(li.textContent);
		}

		var id = number($X('preceding-sibling::a[contains(@id,"city_")]', ul).id);
		var city = { i: island, n: "name", o: "owner"};
		for (var p in city)
			if (isString(city[p]))
				city[p] = item(p);
		for (p in city)
			config.setCity(p, city[p], id);

		var players = config.getServer("players", {});
		var player = players[city.o] = players[city.o] || {};
		player.c = player.c || [];
		if (-1 == player.c.indexOf(id)) {
			player.c.push(id);
			player.c.sort(function(a, b) { return a - b; });
		}
		config.setServer("players", players);
	}
	
	function levelTown() {
		function level(li) {
			var level = integer(li.className);
			var city = $X('a[@onclick]/span', li);
			if (!city) return; // new city site
			var name = $X('text()[preceding-sibling::span]', city);
			if (name) {
				var span = /\S/.test(name.nodeValue) ? name : name.nextSibling;
				var id = integer($X('a', li).id);
				config.setCity(["l", buildingIDs.townHall], level, id);

				// z: 0: normal, 1: vacation, 2: inactive;
				var type = { palm: 1, inactivity: 2 }[span.className] || 0; //FIXED
				config.setCity("z", type, id);
			}
		}
		$x('//li[starts-with(@class,"cityLocation city level")]').forEach(level);
	}
	
	function levelResources() {
		function annotate(what) {
			what = $X('id("islandfeatures")/li['+ what +']');
			if (!what) return;
			var level = number(what.className);
			var id = urlParse("id");
			if (id) {
			var res = what.className.split(" ")[0];
			var resourceIDs = {wood: "w", wine: "W", marble: "M", crystal: "C", sulfur: "S"};
			var rid = resourceIDs[res];
				if ("w" == rid) {
					config.setIsle("w", level, id);
				} else {
					config.setIsle("R", level, id);
					config.setIsle("r", rid, id);
				}
			}
		}
		annotate('contains(@class,"wood")');
		annotate('not(contains(@class,"wood")) and not(@id)');
	}
	
	function coordIsland(island) {
	var breadcrumbs = $X('id("breadcrumbs")/span[.//text()[contains(translate(.,"0123456789:",""),"[]")]]'), x, y, junk;
		if (breadcrumbs) {
			[junk, x, y] = breadcrumbs.textContent.match(/\[(\d+):(\d+)\]/);
			if (island) {
				config.setIsle("x", x = integer(x), island);
				config.setIsle("y", y = integer(y), island);
			}
		}
	}
	
	var island = integer(urlParse("id", $X('id("advCities")//a').search));
	var c = $x('id("cities")/li[contains(@class,"level") and not(contains(@class,"level0"))]/ul[@class="cityinfo"]');
	c.forEach(registerCity);
	levelTown();
	levelResources();
	coordIsland(island);
}
